import time

from selenium.webdriver.common import by
from selenium.webdriver.common.by import By

from app_test.page.base_page import BasePage


class SearchPage(BasePage):
    def search_by_kw(self, kw, expect):
        """
        输入关键进行搜索
        :param kw:
        :return:
        """
        # 2. 点击搜索输入框
        # self.driver.find_element_by_id("sogou.mobile.explorer:id/xp").click()
        # self.find_and_click('//*[@id="sogou.mobile.explorer:id/xp"]')
        # self.find_and_click("//android.widget.TextView[@text='百年奋斗积累了哪些宝贵']")
        self.find_and_click("//android.view.ViewGroup[@resource-id='sogou.mobile.explorer:id/a7k']/android.view.ViewGroup[1]")
        # 3. 向搜索输入框输入 ”阿里巴巴“
        self.find_and_click("//android.widget.EditText[@resource-id='index-kw']")
        # time.sleep(1)
        # self.driver.find_element_by_id("index-kw").click()
        self.find_and_sendkeys("//android.widget.EditText[@resource-id='index-kw']", kw)
        # 4. 搜索结果列表中选择 ”阿里巴巴股票“，然后进行点击
        self.find_and_click("//android.widget.Button[@resource-id='index-bn']")
        # time.sleep(1)
        self.wait_for_element_visible(f"//android.widget.Button[@text='{expect}']")
        self.swipe_find(expect)
        # self.driver.find_element_by_xpath("//*[@resource-id='sogou.mobile.explorer:id/acz'][@text='阿里巴巴股票']").click()
        # time.sleep(1)
        # content_desc = self.driver.find_element_by_xpath("//*[contains(@content-desc, '港元')]").get_attribute(
        #     "content-desc")
        # price = float(content_desc.split("港元")[0])
        # print(price)
        # assert price > 120
        time.sleep(2)
        self.find_and_click("//android.view.View[@resource-id='sogou.mobile.explorer:id/av6']")
        return self